﻿ using Hr.BLL;
using Hr.IBLL;
using Hr.Model;
using HR.HrHelper;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;

namespace HR.Areas.zhao_kuangtao.Controllers
{
    public class ResumeController : ApiController
    {
        protected ResumeListService ResumeList = new ResumeListService();
        int code = 0;
        string msg = "success";

        /// <summary>
        /// 返回简历信息
        /// </summary>
        /// <param name="id">简历信息</param>
        /// <returns>赵匡涛</returns>
        [HttpGet]
        public JObject GetResumeInfo(int page, int limit)
        {
            IEnumerable<object> RList = ResumeList.GetResumeInfo(page, limit).Where(n=>n.Key.Equals("ResumeList")).FirstOrDefault().Value as IEnumerable<object>;
            int Count =int.Parse(ResumeList.GetResumeInfo(page, limit).Where(n => n.Key.Equals("Count")).FirstOrDefault().Value.ToString());
            List<object> ReList = new List<object>();
            foreach (var item in RList)
            {
                string path = "";
                if (item.GetType().GetProperty("ImagePath").GetValue(item) != null)
                {
                    path=item.GetType().GetProperty("ImagePath").GetValue(item).ToString();

                }
                Dictionary<string, object> it= new Dictionary<string, object>()
                {
                    //{"pk_Resume",item.GetType().GetProperty("pk_Resume").GetValue(item).ToString() },
                    //{"ImagePath",FileHelper.GetImgInfo(path) },
                    //{"FullName",item.GetType().GetProperty("FullName").GetValue(item).ToString() },
                    //{"Age",item.GetType().GetProperty("Age").GetValue(item).ToString() },
                    //{"TypeNmae_CHS",item.GetType().GetProperty("TypeNmae_CHS").GetValue(item).ToString() },
                    //{"AnimalSign",item.GetType().GetProperty("AnimalSign")==null?"":item.GetType().GetProperty("AnimalSign").GetValue(item).ToString() },
                    //{"Province",item.GetType().GetProperty("Province").GetValue(item).ToString() },
                    //{"Birthday",item.GetType().GetProperty("Birthday").GetValue(item).ToString() },
                    //{"StartDate",item.GetType().GetProperty("StartDate").GetValue(item).ToString() },
                    //{"EndDate",item.GetType().GetProperty("EndDate").GetValue(item).ToString() },
                    //{"Description",item.GetType().GetProperty("Description").GetValue(item).ToString() },
                    //{"StatusNmae_CHS",item.GetType().GetProperty("StatusNmae_CHS").GetValue(item).ToString() }
                    {"ImagePath",FileHelper.GetImgInfo(path) },
                    {"item",item }
                };
                ReList.Add(it);
            
            }
            
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code },
                {"msg",msg },
                {"data",new{ ResumeList=ReList,Count=Count} }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
        /// <summary>
        /// 模糊查询简历信息
        /// </summary>
        /// <param name="id">简历信息</param>
        /// <returns>赵匡涛</returns>
        [HttpGet]
        public JObject GetResume_ListAll(string Name, int page, int limit)
        {
            if (Name == null)
            {
                Name = "";
            }
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code },
                {"msg",msg },
                {"data",ResumeList.GetResume_ListAll(Name,page,limit) }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
        [HttpGet]
        public JObject ResumeListInfo(int ResumeID)
        {
            Resume_List R = ResumeList.ResumeListInfo(ResumeID);

            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code },
                {"msg",msg },
                {"data",R },
                {"ImgDt",FileHelper.GetImgInfo(R.ImagePath) }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }

        public JObject TypeListANStatusList()
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code },
                {"msg",msg },
                {"data",ResumeList.TypeListANStatusList()
        }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
        /// <summary>
        /// 编辑 添加 简历信息
        /// </summary>
        /// <param name="id">简历信息</param>
        /// <returns>赵匡涛</returns>
        /// 
        [HttpPost]
        public JObject UpdateResume()
        {
            System.Collections.Specialized.NameValueCollection data = HttpContext.Current.Request.Form;
            // 调用jsonhelper帮助类转换成对象
            Resume_List r = JsonHelper.DeserializeJsonToObject<Resume_List>(data["Resume"]);
            string type = data["type"];
            string pk_User = data["pk_User"];
            if (type.Equals("Update"))
            {
                   Resume_List SelRo = ResumeList.ResumeListInfo(r.pk_Resume);
                msg = "更新成功";
                if (SelRo == null)
                {
                    code = 1;
                    msg = "角色不存在";
                }
                else
                {
                    //两个对象对比是否相等
                    if (ModelHelper.ReflectModel(r, SelRo))
                    {
                        msg = "无任何更新";
                    }
                    else
                    {
                        if (!ResumeList.UpdateResume(r, type,int.Parse(pk_User)))
                        {
                            code = 1;
                            msg = "更新失败";
                        }
                    }
                }
            }
            else
            {
                msg = "添加成功";

                if (!ResumeList.UpdateResume(r, type,int.Parse(pk_User)))
                {
                    code = 1;
                    msg = "添加失败";
                }
            }
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }

            [HttpPost]
        public JObject DelResume_List(int pk_Resume)
        {
            Resume_List r = ResumeList.ResumeListInfo(pk_Resume);
            msg = "删除成功";
            if (r != null)
            {
                if (!ResumeList.DelResume_List(r))
                {
                    //失败
                    code = 1;
                    msg = "删除失败";
                }
            }
            else
            {
                code = 1;
                msg = "角色不存在";
            }
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
    }
}
